<template>
  <div style="height: 6vh;background-color:#fff;padding: 10px 20px;">
    <span style="line-height:60px;font-size:20px;">店铺管理</span>
    <el-button type="primary" style="float: right;margin-top: 10px;"
               @click="beforeAddShop">新建店铺</el-button>
  </div>
  <!-- 新建店铺弹窗 -->
  <el-dialog
      :title="dialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="dialogVisible"
      :before-close="handleClose"
  >
    <el-form label-position="top" label-width="80px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="店铺名称">
            <el-input placeholder="请输入店铺名称" v-model="saveShopForm.name"
                      :disabled="saveShopForm.id != null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="位置">
            <el-input placeholder="请输入位置" v-model="saveShopForm.location"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="店长姓名">
            <el-input placeholder="请输入店长姓名" v-model="saveShopForm.managerName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话">
            <el-input placeholder="请输入联系电话" v-model="saveShopForm.contactPhone"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="开业日期">
            <el-date-picker
                v-model="saveShopForm.openingDate"
                type="date"
                placeholder="选择日期"

            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="面积">
            <el-input placeholder="请输入面积" v-model="saveShopForm.size"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="状态">
            <el-radio-group v-model="saveShopForm.status">
              <el-radio border label="启用" value="1" style="margin:0;"></el-radio>
              <el-radio border label="禁用" value="0"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="员工数量">
            <el-input-number v-model="saveShopForm.empeNum" :min="0" :max="1000"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="店铺简称">
            <el-input placeholder="请输入店铺简称" v-model="saveShopForm.shortShopName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveShop">保存</el-button>
    </template>
  </el-dialog>

  <!-- 店铺搜索卡片 -->
  <el-card style="margin: 20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="店铺搜索" style="width: 220px;">
        <el-input placeholder="输入店铺名称" v-model="searchShopForm.name" @keydown.enter="loadShops"></el-input>
      </el-form-item>
      <el-form-item label="店铺状态" style="width: 220px;">
        <el-select placeholder="请选择店铺状态" v-model="searchShopForm.status" @change="loadShops">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadShops">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>

  <!-- 店铺表格卡片 -->
  <el-card style="margin: 20px;">
    <el-table :data="shopArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="店铺名称" align="center"></el-table-column>
      <el-table-column prop="location" label="位置" align="center"></el-table-column>
      <el-table-column prop="managerName" label="店长姓名" align="center"></el-table-column>
      <el-table-column prop="contactPhone" label="联系电话" align="center"></el-table-column>
      <el-table-column prop="openingDate" label="开业日期" align="center"></el-table-column>
      <el-table-column prop="size" label="面积" align="center"></el-table-column>
      <el-table-column label="状态" align="center">
        <template #default="scope">
          <el-switch active-value="1" inactive-value="0"
                     v-model="scope.row.status" @change="changeShopStatus(scope.row.id,scope.row.status)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="empeNum" label="员工数量" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary"
                     @click="editShop(scope.row.id)">编辑</el-button>
          <el-button link type="danger"
                     @click="deleteShop(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import axios from 'axios';
import qs from 'qs';
import { ElMessage } from 'element-plus';


// 定义变量控制弹窗是否出现
const dialogVisible = ref(false);
// 定义变量用来控制弹窗的标题
const dialogTitle = ref('新建店铺');

// 定义对象用来收集保存表单的所有店铺数据
const saveShopForm = ref({
  name: '',
  shortShopName:'',
  location: '',
  managerName: '',
  contactPhone: '',
  openingDate: '',
  size: '',
  status: '1',
  empeNum: ''
});

// 点击"新建店铺"按钮, 出现弹窗+修改弹窗标题
const beforeAddShop = () => {
  dialogVisible.value = true;
  dialogTitle.value = '新建店铺';
  saveShopForm.value = {
    name: '',
    shortShopName:'',
    location: '',
    managerName: '',
    contactPhone: '',
    openingDate: '',
    size: '',
    status: '1',
    empeNum: ''
  };
};

// 定义对象保存查询到的店铺表格数据
const shopArr = ref([]);

// 定义加载店铺的方法
const loadShops = () => {
  const params = qs.stringify(searchShopForm.value);
  axios.get(`${BASE_URL}/v1/shop/select?${params}`)
      .then((response) => {
        if (response.data.code === 2000) {
          shopArr.value = response.data.data;
        } else {
          ElMessage.error(response.data.msg);
        }
      });
};

// 页面一加载完毕,立即执行
onMounted(() => {
  loadShops();
});

// 定义保存店铺的方法
const saveShop = () => {
  const data = saveShopForm.value;
  if (data.id) {
    // 更新店铺
    axios.post(`${BASE_URL}/v1/shop/update`, data)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('更新成功!');
            dialogVisible.value = false;
            loadShops();
          } else {
            ElMessage.error(response.data.msg);
          }
        });
  } else {
    // 新增店铺
    axios.post(`${BASE_URL}/v1/shop/add`, data)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('新增成功!');
            dialogVisible.value = false;
            loadShops();
          } else {
            ElMessage.error(response.data.msg);
          }
        });
  }
};

// 处理弹窗关闭的方法
const handleClose = () => {
  if (confirm('您确认要关闭本窗口吗？')) {
    dialogVisible.value = false;
  }
};

// 定义对象用来保存店铺的查询条件
const searchShopForm = ref({ name: '', status: '' });




// 定义重置搜索条件的方法
const resetSearch = () => {
  searchShopForm.value = { name: '', status: '' };
  loadShops();
};

//修改店铺状态的方法
const changeShopStatus = (id, status) => {
  axios.post(`${BASE_URL}/v1/shop/updateStatus/${id}/${status}`)
      .then((response) => {
        if (response.data.code === 2000) {
          ElMessage.success('操作成功!');
        } else {
          ElMessage.error(response.data.msg);
        }
      });
};
// 编辑店铺的方法
const editShop = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑店铺';
  axios.get(`${BASE_URL}/v1/shop/select?id=${id}`)
      .then((response) => {
        if (response.data.code === 2000) {
          saveShopForm.value = response.data.data[0];
        } else {
          ElMessage.error(response.data.msg);
        }
      });
};

// 删除店铺的方法
const deleteShop = (id) => {
  axios.post(`${BASE_URL}/v1/shop/delete/${id}`)
      .then((response) => {
        if (response.data.code === 2000) {
          ElMessage.success('删除成功!');
          loadShops(); // 重新加载店铺数据
        } else {
          ElMessage.error(response.data.msg);
        }
      });
};


</script>

<style scoped>
</style>













